import Vue from 'vue'
import Vuex from 'vuex'
import user from './modules/user'
import user1 from './modules/user1'
Vue.use(Vuex)

export default new Vuex.Store({
  strict: true,
  state: {
    count: 100,
    name: '张三',
    list: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
  },
  getters: {
  },
  mutations: {
    //只能通过mutations里面改变state
    add(state, num) {
      if (num) {
        state.count += num
      } else {
        state.count++
      }
    },
    reduce(state) {
      state.count--
    },
    rename(state, name) {
      state.name = name
    }
  },
  actions: {
    //存放异步操作
    addAsync({ commit }, num) {
      setTimeout(() => {
        console.log(this)
        // this.commit('add')
        commit('add', num)
      }, 1000)
    }
  },
  getters: {
    total(state) {
      return state.list.reduce((pre, cur) => {
        return pre + cur
      })
    }
  },
  modules: {
    //模块名:导入的模块
    user,
    user1
  }
})
